<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="交易订单流水号" prop="orderId" label-width="200px;">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入交易订单流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="merName" label-width="200px;">
        <el-input
          v-model="queryParams.merName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户手机号" prop="userPhone" label-width="200px;">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易金额" prop="transAmount" label-width="200px;">
        <el-input
          v-model="queryParams.transAmount"
          placeholder="请输入交易金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理结果" prop="handleResult" label-width="200px;">
        <el-input
          v-model="queryParams.handleResult"
          placeholder="请输入处理结果"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="checkStatus">
        <el-select v-model="queryParams.handleStaus" placeholder="请选择处理状态" clearable size="small">
          <el-option label="--请选择--" value="" />
          <el-option v-for="(value, key) in handleStausList" :key="key" :value="key" :label="value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="交易类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择交易类型" clearable size="small">
          <el-option label="--请选择--" value="" />
          <el-option v-for="(value, key) in payTypeList" :key="key" :value="key" :label="value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="终端SN号" prop="snCode" label-width="200px;">
        <el-input
          v-model="queryParams.snCode"
          placeholder="请输入终端SN号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <br>
      <el-form-item label="交易时间" prop="queryCreateTimeStr" label-width="200px;">
        <el-date-picker
          v-model="queryParams.queryCreateTimeStr"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['pos:TTerminalTransFlowInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="TTerminalTransFlowInfoList"  :summary-method="getSummaries" show-summary  @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="数据主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="交易订单流水号" align="center" prop="orderId" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="用户手机号" align="center" prop="userPhone" />
      <el-table-column label="终端SN号" align="center" prop="snCode" />
      <el-table-column label="交易金额" align="center" prop="transAmount" />
      <el-table-column label="交易类型" align="center" prop="payType" />
      <el-table-column label="处理状态" align="center" prop="handleStaus" />
      <el-table-column label="处理结果" align="center" prop="handleResult" />
      <el-table-column label="交易时间" align="center" prop="transTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['pos:TTerminalTransFlowInfo:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {queryPayType, queryHandleStatus, listTTerminalTransFlowInfo, getTTerminalTransFlowInfo, delTTerminalTransFlowInfo, addTTerminalTransFlowInfo, updateTTerminalTransFlowInfo, exportTTerminalTransFlowInfo } from "@/api/pos/TTerminalTransFlowInfo";

export default {
  name: "TTerminalTransFlowInfo",
  data() {
    return {
      handleStausList: [],
      payTypeList: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 终端交易流水表格数据
      TTerminalTransFlowInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentId: null,
        orderId: null,
        merName: null,
        merId: null,
        snCode: null,
        userPhone: null,
        transAmount: null,
        payType: null,
        handleResult: null,
        handleStatus: null,
        queryCreateTimeStr: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.queryPayType();
    this.queryHandleStatus();
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        }
          // index >= 3 || index <= 11 是你需要计算的列
          // 当然也可以指定一个或者多个列
        // index === 2 || index === 3 这样值求和第二个与第三个列
        else if (index == 4) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                // 保存了两位小数点
                return Math.floor((prev + curr) * 100) / 100;
              } else {
                // 保存了两位小数点
                return Math.floor(prev * 100) / 100;
              }
            }, 0)
          } else {
            sums[index] = 'N/A'
          }
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    /** 查询终端交易流水列表 */
    getList() {
      this.loading = true;
      listTTerminalTransFlowInfo(this.queryParams).then(response => {
        this.TTerminalTransFlowInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    queryPayType(){
      queryPayType().then(response => {
        this.payTypeList = response.data;
      });
    },
    queryHandleStatus(){
      queryHandleStatus().then(response => {
        this.handleStausList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        agentId: null,
        machineType: null,
        orderId: null,
        sysno: null,
        merName: null,
        merId: null,
        snCode: null,
        transAmount: null,
        merSigeFee: null,
        extractionFee: null,
        settlementCycle: "0",
        payType: null,
        cardType: null,
        transCategory: null,
        cardNo: null,
        createTime: null,
        isValid: "0",
        handleStaus: null,
        handleResult: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加终端交易流水";
    },
    /** 查询详情按钮操作 */
    handleDetail(row) {
      const id = row.id || this.ids[0];
      //console.log("----------", id);
      this.$router.push({path: "/TTerminalTransFlowInfo/transManage/transDetail", query: {id: id, pageNum: this.queryParams.pageNum}});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTTerminalTransFlowInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改终端交易流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTTerminalTransFlowInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTTerminalTransFlowInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delTTerminalTransFlowInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有终端交易流水数据项？').then(() => {
        this.exportLoading = true;
        return exportTTerminalTransFlowInfo(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
